package com.song.list.controller;

import com.alibaba.fastjson.JSONObject;
import com.song.common.constant.RedisConstants;
import com.song.common.result.ResultEntity;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.list.entity.WhiteList;
import com.song.list.service.WhiteListService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.stream.Collectors;

/**
* <p>
    * 请求路径白名单 控制器
    * </p>
*
* @author song
* @since 2023-10-31
*/
@Api(tags = {"请求路径白名单 控制器"})
@RestController
@RequestMapping("/white_list")
public class WhiteListController {

    @Autowired
    private WhiteListService whiteListService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
    * 分页查询数据
    *
    * @author song
    * @Date 2023-10-31
    */
    @ApiOperation(value = "按条件分页检索请求路径白名单",notes = "按条件分页检索请求路径白名单")
    @GetMapping(value = "/pageList")
    public ResultEntity<Page<WhiteList>> pageList(
        @Validated WhiteList whiteList,
        @ApiParam(value = "页码", defaultValue = "1")
        @RequestParam(defaultValue = "1") int pageNum,
        @ApiParam(value = "分页大小", defaultValue = "10")
        @RequestParam(defaultValue = "10") int pageSize
    ) {
        Page<WhiteList> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<WhiteList> lambda = new QueryWrapper<WhiteList>().lambda();
        if(ObjectUtils.isNotEmpty(whiteList.getId())){
            lambda.eq(WhiteList::getId,whiteList.getId());
        }
        if(ObjectUtils.isNotEmpty(whiteList.getRouteType())){
            lambda.eq(WhiteList::getRouteType,whiteList.getRouteType());
        }
        if(ObjectUtils.isNotEmpty(whiteList.getPath())){
            lambda.eq(WhiteList::getPath,whiteList.getPath());
        }
        if(ObjectUtils.isNotEmpty(whiteList.getComment())){
            lambda.eq(WhiteList::getComment,whiteList.getComment());
        }
        if(ObjectUtils.isNotEmpty(whiteList.getCreateTime())){
            lambda.eq(WhiteList::getCreateTime,whiteList.getCreateTime());
        }
        if(ObjectUtils.isNotEmpty(whiteList.getUpdateTime())){
            lambda.eq(WhiteList::getUpdateTime,whiteList.getUpdateTime());
        }
        return ResultEntity.success(whiteListService.page(page,lambda));
    }

    /**
    * 查询数据List
    *
    * @author song
    * @Date 2023-10-31
    */
    @ApiOperation(value = "查询全部请求路径白名单",notes = "查询全部请求路径白名单")
    @GetMapping(value = "/list")
    public ResultEntity<List<WhiteList>> list() {
        List<Object> values = redisTemplate.opsForHash().values(RedisConstants.WHITE_LIST_KEY);
        if (!CollectionUtils.isEmpty(values)){
            List<WhiteList> whiteLists = values.stream().map(obj -> {
                return JSONObject.parseObject(obj.toString(), WhiteList.class);
            }).collect(Collectors.toList());
            return ResultEntity.success(whiteLists);
        }
        List<WhiteList> whiteListList = whiteListService.list();
        return ResultEntity.success(whiteListList);
    }

    /**
    * 请求路径白名单新增
    *
    * @author song
    * @Date 2023-10-31
    */
    @ApiOperation(value = "新增请求路径白名单",notes = "新增请求路径白名单")
    @PostMapping(value = "/add")
    public ResultEntity add(@RequestBody WhiteList whiteList) {
        boolean flag = whiteListService.save(whiteList);
        if (flag){
            redisTemplate.opsForHash().put(RedisConstants.WHITE_LIST_KEY, whiteList.getId(), JSONObject.toJSONString(whiteList));
        }
        return ResultEntity.success();
    }

    /**
    * 请求路径白名单修改
    *
    * @author song
    * @Date 2023-10-31
    */
    @ApiOperation(value = "修改请求路径白名单",notes = "修改请求路径白名单")
    @PostMapping(value = "/update")
    public ResultEntity edit(@RequestBody WhiteList whiteList) {
        boolean flag = whiteListService.updateById(whiteList);
        if (flag){
            redisTemplate.opsForHash().put(RedisConstants.WHITE_LIST_KEY, whiteList.getId(), JSONObject.toJSONString(whiteList));
        }
        return ResultEntity.success();
    }

    /**
    * 删除请求路径白名单
    *
    * @author song
    * @Date 2023-10-31
    */
    @ApiOperation(value = "按id删除请求路径白名单",notes = "按id删除请求路径白名单")
    @PostMapping(value = "/delete")
    public ResultEntity delete(@RequestParam Serializable id) {
        boolean flag = whiteListService.removeById(id);
        if (flag){
            redisTemplate.opsForHash().delete(RedisConstants.WHITE_LIST_KEY, id);
        }
        return ResultEntity.success();
    }


    /**
    * 根据id查询请求路径白名单
    *
    * @author song
    * @Date 2023-10-31
    */
    @ApiOperation(value = "根据id查询请求路径白名单",notes = "根据id查询修改请求路径白名单")
    @GetMapping(value = "/detail")
    public ResultEntity<WhiteList> detail(@RequestParam Serializable id) {
        Object obj = redisTemplate.opsForHash().get(RedisConstants.WHITE_LIST_KEY, id);
        if (obj != null){
            WhiteList whiteList = JSONObject.parseObject(obj.toString(), WhiteList.class);
            return ResultEntity.success(whiteList);
        }
        WhiteList whiteList = whiteListService.getById(id);
        return ResultEntity.success(whiteList);
    }

    @PostConstruct
    public void initWhiteList(){
        List<WhiteList> whiteLists = whiteListService.list();
        if (!CollectionUtils.isEmpty(whiteLists)){
            whiteLists.stream().forEach(whiteList -> {
                redisTemplate.opsForHash().put(RedisConstants.WHITE_LIST_KEY, whiteList.getId(), JSONObject.toJSONString(whiteList));
            });
        }
    }
}